#!/usr/bin/perl

use strict;
use warnings;
use Pod::Usage;

our %Commands;
our %Settings;
our $OS_win;

our %Aliases;

our %ParameterRegExpValues;

our $LastCommandType;    #

&msg( '0110D', 'Built-in commands' );

&msg( '0110D', 'SHow ENVironment' );
$Commands{&commandRegexp( 'show', 'environment' )} = sub {

    if ( $ParameterRegExpValues{HELP} ) {
        ###############################
        # Put your help message here! #
        ###############################
        print "--------\n";
        print "SHow ENVironment Help!\n";
        print "--------\n";

        return 0; # Ok
    }

    my @tmpArray;

    for my $key ( sort( keys %Settings ) ) {
        push( @tmpArray, "$key\t=\t"."[".&colorString( $Settings{$key}, "BOLD YELLOW" ).&colorString( "]", "BOLD WHITE" ) );
    }

    &setSimpleTXTOutput();
    &universalTextPrinter( "KEY[GREEN]\t\tVALUE[BOLD WHITE]", &grepIt ( @tmpArray ) );

    return 0; # matched

};

&msg( '0110D', 'SHow RULer' );
$Commands{&commandRegexp( 'show', 'ruler' )} = sub {

    if ( $ParameterRegExpValues{HELP} ) {
        ###############################
        # Put your help message here! #
        ###############################
        print "--------\n";
        print "SHow RULer Help!\n";
        print "--------\n";

        return 0; # Ok
    }

    &showRuler();

    return 0; # matched

};
&defineAlias( 'ruler', 'show ruler' );

&msg( '0110D', 'RELoad' );
$Commands{&commandRegexp( 'reload', '' )} = sub {

    if ( $ParameterRegExpValues{HELP} ) {
        ###############################
        # Put your help message here! #
        ###############################
        print "--------\n";
        print "REload Help!\n";
        print "--------\n";

        return 0; # Ok
    }

    &reLoadPlugins();

    return 0; # matched

};

&msg( '0110D', 'ALIas' );
$Commands{&commandRegexp( 'alias', '' )} = sub {

    if ( $ParameterRegExpValues{HELP} ) {
        ###############################
        # Put your help message here! #
        ###############################
        print <<ALIASHELPEND;
alias [name=value]

With name=value specified, define name as an alias and assign it the value value. A trailing space in value causes the next word to be checked for alias substitution.

With name=value omitted, print the list of aliases in the form name=value on standard output.
ALIASHELPEND

        return 0;
    }

    if ( !defined $2 || $2 eq '' ) {
        my @returntmp;
        foreach my $key ( sort keys %Aliases ) {
            push( @returntmp, "$key\t=\t$Aliases{$key}" ) if ( defined( $Aliases{$key} ) );
        }

        &setSimpleTXTOutput();
        &universalTextPrinter( "ALIAS\t\tCOMMAND", &grepIt ( @returntmp ) );

    }
    else {
        #my ( $alias, $command ) = split( /\s*=\s*/, $2 );
        my $parameter = $2." ".$3." ".$4." ".$5." ".$6." ".$7;
        if ( $parameter =~ m/\s*(.+)\s*=\s*(.+)\s*/ ) {
            my $alias   = $1;
            my $command = $2;
            &defineAlias( $alias, $command ) if ( $alias  ne '' );
            delete $Aliases{$alias}          if ( ! defined( $command ) || $command eq '' );
        }
    }

    return 0;

};

##################################################################

&msg( '0110D', 'SEt SERver' );
$Commands{qr/^(set|se)\s+(server|serve|serv|ser)\s*(\S*)/i} = sub {

    if ( $ParameterRegExpValues{HELP} ) {
        ###############################
        # Put your help message here! #
        ###############################
        print "--------\n";
        print "set Server Help!\n";
        print "--------\n";
        pod2usage(
            -input    => "plugins/builtin_commands.pl",
            -exitval  => 'NOEXIT',
            -verbose  => 99,
            -sections => [ "SETSERVER", "NAME" ]
        );
        return 0;
    }

    &setServer($3);

    &updatePrompt();

    return 0;

};
&defineAlias( 'server', 'set server' );

$Commands{qr/^(console|consol|conso|cons|con)\s*(\S*)/i} = sub {

    if ( $ParameterRegExpValues{HELP} ) {
        ###############################
        # Put your help message here! #
        ###############################
        print "--------\n";
        print "Console Help!\n";
        print "--------\n";
        pod2usage( -verbose => 3, -sections => "CONSOLE" );
        return 0;
    }
    &startConsole;
    return 0;

};

1;

__END__

=pod

=head SETSERVER

 SEt SERver C<KORNYEZET>

=head2 NAME

SEt SERver

=head1 SYNOPSIS

SEt SERver I<server név>

=head1 DESCRIPTION

Beállítja a TSM szerverhez történő csatlakozáshoz szükséges paramétereket


=head CONSOLE

=head1 NAME

CONsole


=head1 SYNOPSIS

CONsole

=head1 DESCRIPTION


Megjeleníti a dsmadmc konzol ablakát


